<?php

namespace app\models\comment;

use app\models\user\User;
use Yii;

/**
 * This is the model class for table "comment".
 *
 * @property int $id
 * @property int $order_id
 * @property int $user_id
 * @property string|null $content
 * @property int $check 审核 0 未审核 1 通过
 * @property string $create_time
 * @property string $update_time
 * @property int $product_id
 */
class Comment extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'comment';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['order_id', 'user_id', 'check', 'product_id'], 'integer'],
            [['content'], 'string'],
            [['create_time', 'update_time'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'order_id' => 'Order ID',
            'user_id' => 'User ID',
            'content' => 'Content',
            'check' => 'Check',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
        ];
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            $time = date('Y-m-d H:i:s');
            $this->update_time = $time;
            if ($insert) {
                $this->create_time = $time;
            }
            return true;
        }
        return false;
    }

    public function getImages()
    {
        return $this->hasMany(CommentImage::class, ['comment_id' => 'id']);
    }

    public function getUser()
    {
        return $this->hasOne(User::class, ['id' => 'user_id'])->select(['id', 'mobile', 'nickname', 'avatar']);
    }
}
